//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartThumbnailsView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartThumbnailsView.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartThumbnailsView

IMPLEMENT_DYNCREATE(CChartThumbnailsView, CBCGPChartExampleView)

CChartThumbnailsView::CChartThumbnailsView()
	: CBCGPChartExampleView(CChartThumbnailsView::IDD)
{
	//{{AFX_DATA_INIT(CChartThumbnailsView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	for (int i = 0; i < CHART_THUMBNAIL_NUM; i++)
	{
		m_pCharts[i] = NULL;
	}

	m_pClickedChart = NULL;
	m_pExpandedChart = NULL;
	m_pInfoText = NULL;
}

CChartThumbnailsView::~CChartThumbnailsView()
{
}

void CChartThumbnailsView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartThumbnailsView)
	DDX_Control(pDX, IDC_CHART, m_wndContainer);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartThumbnailsView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartThumbnailsView)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
	ON_REGISTERED_MESSAGE(BCGM_ON_CHART_MOUSE_DOWN, OnChartMouseDown)
	ON_REGISTERED_MESSAGE(BCGM_ON_CHART_MOUSE_UP, OnChartMouseUp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartThumbnailsView diagnostics

#ifdef _DEBUG
void CChartThumbnailsView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartThumbnailsView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartThumbnailsView message handlers

static BCGPChartCategory categories[] =
{
	BCGPChartLine,
	BCGPChartPie,
	BCGPChartPyramid,
	BCGPChartColumn,
	BCGPChartBar,
	BCGPChartArea,
	BCGPChartBubble,
	BCGPChartDoughnut,
	BCGPChartColumn3D,
	BCGPChartBar3D,
	BCGPChartArea3D,
};

void CChartThumbnailsView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}
	
	m_bIsReady = TRUE;
	
	CBCGPVisualContainer* pContainer = m_wndContainer.GetVisualContainer();
	ASSERT_VALID(pContainer);

	m_pInfoText = new CBCGPTextGaugeImpl(_T(""), CBCGPColor::DarkGray, pContainer);

	CBCGPTextFormat tf = m_pInfoText->GetTextFormat();
	tf.SetTextAlignment(CBCGPTextFormat::BCGP_TEXT_ALIGNMENT_LEADING);
	tf.SetEndEllipsis();

	m_pInfoText->SetTextFormat(tf);
	
	for (int i = 0; i < CHART_THUMBNAIL_NUM; i++)
	{
		m_pCharts[i] = new CBCGPChartVisualObject(pContainer);
		m_pCharts[i]->SetThumbnailMode();

		m_pCharts[i]->SetChartType(categories[i]);

		if (categories[i] == BCGPChartBubble)
		{
			m_pCharts[i]->AddChartDataYXY1(_T("California"), 3, 2, 6700);
			m_pCharts[i]->AddChartDataYXY1(_T("Virginia"), 4, 3, 5500);
			m_pCharts[i]->AddChartDataYXY1(_T("New York"), 5, 4, 3200);
			m_pCharts[i]->AddChartDataYXY1(_T("Pennsylvania"), 6, 5, 2500);
			m_pCharts[i]->AddChartDataYXY1(_T("Georgia"), 5, 6, 1400);
			m_pCharts[i]->AddChartDataYXY1(_T("Colorado"), 4, 7, 1350);
			m_pCharts[i]->AddChartDataYXY1(_T("Other States"), 8, 7.5, 1700);

			CBCGPChartBubbleSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartBubbleSeries, m_pCharts[i]->GetSeries(0));
			if (pSeries != NULL)
			{
				ASSERT_VALID(pSeries);
				pSeries->SetBubbleScale(300);
				pSeries->EnableAutoColorDataPoints();
			}
		}
		else
		{
			int nDataPoints = 8;

			if (categories[i] == BCGPChartLine || categories[i] == BCGPChartArea)
			{
				nDataPoints = 30;
			}

			for (int j = 0; j < nDataPoints; j++)
			{
				m_pCharts[i]->AddChartData(Rand(0, 99));
			}
		}
	}

	Repos();

	CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
	UpdateChartColorTheme(pMainFrame->GetColorTheme(), pMainFrame->IsDarkTheme());
}

void CChartThumbnailsView::UpdateChartColorTheme(int nTheme, BOOL bIsDarkTheme)
{
	CBCGPBrush brFill;

	for (int i = 0; i < CHART_THUMBNAIL_NUM; i++)
	{
		if (m_pCharts[i] != NULL)
		{
			if (nTheme < 0)
			{
				m_pCharts[i]->SetColors(theApp.GetCustomTheme(nTheme), bIsDarkTheme);
			}
			else
			{
				m_pCharts[i]->SetColors((CBCGPChartTheme::ChartTheme)nTheme, bIsDarkTheme);
			}

			if (brFill.IsEmpty())
			{
				brFill = m_pCharts[i]->GetDiagramFillColor();
			}
		}
	}

	if (m_pInfoText != NULL)
	{
		m_pInfoText->SetTextColor(bIsDarkTheme ? CBCGPColor::LightGray : CBCGPColor::DarkGray);
	}

	m_wndContainer.GetVisualContainer()->SetFillBrush(brFill);
	m_wndContainer.RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_UPDATENOW | RDW_ALLCHILDREN);
}

void CChartThumbnailsView::OnSize(UINT nType, int cx, int cy) 
{
	CBCGPChartExampleView::OnSize(nType, cx, cy);
	Repos();
}

void CChartThumbnailsView::Repos()
{
	CBCGPVisualContainer* pContainer = m_wndContainer.GetVisualContainer();
	ASSERT_VALID(pContainer);
	
	if (pContainer == NULL || pContainer->GetCount() == 0)
	{
		return;
	}

	const double cx = globalUtils.ScaleByDPI(120);
	const double cy = globalUtils.ScaleByDPI(100);
	const double margin = globalUtils.ScaleByDPI(10);
	const double captionHeight = globalUtils.ScaleByDPI(20);

	CBCGPRect rect = pContainer->GetRect();
	
	CBCGPRect rectCaption = rect;
	rectCaption.right -= 2 * margin;
	rectCaption.bottom = rectCaption.top + captionHeight + margin;
	
	m_pInfoText->SetRect(rectCaption);

	rect.top = rectCaption.bottom;

	if (m_pExpandedChart != NULL)
	{
		ASSERT_VALID(m_pExpandedChart);
		m_pExpandedChart->SetRect(rect);

		m_pInfoText->SetText(_T(" Click chart to return back to the thumbnail view."));
	}
	else
	{
		m_pInfoText->SetText(_T(" Click thumbnail to see a full-size chart."));
	}

	double x = rect.left;
	double y = rect.top;
	
	for (int i = 0; i < CHART_THUMBNAIL_NUM; i++)
	{
		if (m_pExpandedChart != NULL)
		{
			if (m_pCharts[i] != m_pExpandedChart)
			{
				m_pCharts[i]->SetVisible(FALSE);
			}
		}
		else
		{
			m_pCharts[i]->SetVisible();

			CBCGPRect rectChart(x, y, x + cx, y + cy);
			rectChart.DeflateRect(margin / 2, margin / 2);

			m_pCharts[i]->SetRect(rectChart);

			x += cx;

			if (x + cx > rect.right)
			{
				x = rect.left;
				y += cy;
			}
		}
	}
	
	m_wndContainer.RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_UPDATENOW | RDW_ALLCHILDREN | RDW_ERASE);
}

LRESULT CChartThumbnailsView::OnChartMouseDown(WPARAM /*wp*/, LPARAM lp)
{
	if (m_pExpandedChart != NULL)
	{
		ASSERT_VALID(m_pExpandedChart);

		m_pExpandedChart->SetThumbnailMode();
		m_pExpandedChart = NULL;
		Repos();

		return 0;
	}

	CBCGPVisualContainer* pContainer = m_wndContainer.GetVisualContainer();
	ASSERT_VALID(pContainer);

	BCGPChartHitInfo* pHitInfo = (BCGPChartHitInfo*)lp;
	if (pHitInfo->m_nMouseButton != 0)
	{
		return 0;
	}

	m_pClickedChart = DYNAMIC_DOWNCAST(CBCGPChartVisualObject, pContainer->GetFromPoint(pHitInfo->m_ptHit));
	return 1;
}

LRESULT CChartThumbnailsView::OnChartMouseUp(WPARAM /*wp*/, LPARAM lp)
{
	CBCGPVisualContainer* pContainer = m_wndContainer.GetVisualContainer();
	ASSERT_VALID(pContainer);
	
	BCGPChartHitInfo* pHitInfo = (BCGPChartHitInfo*)lp;
	if (pHitInfo->m_nMouseButton != 0)
	{
		return 0;
	}

	CBCGPChartVisualObject* pClickedChart = DYNAMIC_DOWNCAST(CBCGPChartVisualObject, pContainer->GetFromPoint(pHitInfo->m_ptHit));

	if (m_pClickedChart != pClickedChart || pClickedChart == NULL)
	{
		m_pClickedChart = NULL;
		return 0;
	}

	m_pExpandedChart = m_pClickedChart;
	m_pExpandedChart->SetThumbnailMode(FALSE);

	m_pClickedChart = NULL;

	Repos();
	
	return 1;
}
